#include "BlockQueue.hpp"
#include<time.h>
#include<cstdlib>
#include<unistd.h>
#include<sys/types.h>
#include "Task.hpp"
using namespace sola_blockqueue;
void* Consumer(void* _bq)
{
    BlockQueue<Task>* bq = reinterpret_cast<BlockQueue<Task>*>(_bq);
    //不断消费
    while(true)
    {
        Task t;
        bq->Pop(&t);
        std::cout<<"消费者：";
        t();
    }
    return nullptr;
}

void* Producter(void* _bq)
{
    BlockQueue<Task>* bq = reinterpret_cast<BlockQueue<Task>*>(_bq);
    //不断生产
    while(true)
    {
        sleep(1);
        //制造资源
        //生产者数据（task）从哪来
        int x = rand()%20 +1;
        int y = rand()%10 +1;
        char op = "+-*/%"[rand()%5]; //随机取一个操作符
        Task t(x,y,op);
        std::cout<<"生产者生产了一个方法"<<std::endl;
        bq->Push(t);
    }
    return nullptr;

}

int main()
{
    srand(time(nullptr)^getpid());
    pthread_t tid_1,tid_2;
    BlockQueue<Task>* bq = new BlockQueue<Task>;

    pthread_create(&tid_1,nullptr,Consumer,reinterpret_cast<void*>(bq));
    pthread_create(&tid_2,nullptr,Producter,reinterpret_cast<void*>(bq));

    pthread_join(tid_1,nullptr);
    pthread_join(tid_2,nullptr);

    return 0;
}